package com.youlu.campus.service.english;

import com.youlu.campus.entity.english.CompetitionConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class CompetitionConfigService {

    @Autowired
    private MongoTemplate mongoTemplate;


    public void save(CompetitionConfig competitionConfig){
        String id = competitionConfig.getId();
        if(StringUtils.isBlank(id)){
            competitionConfig.setCreatedTime(new Date());
            this.mongoTemplate.save(competitionConfig);
            return;
        }
        CompetitionConfig competitionConfigExist = this.mongoTemplate.findById(id, CompetitionConfig.class);
        BeanUtils.copyProperties(competitionConfig,competitionConfigExist);
        this.mongoTemplate.save(competitionConfigExist);
    }
    public CompetitionConfig detail(){
        return this.mongoTemplate.findOne(new Query(),CompetitionConfig.class);
    }


}
